package com.mxchip.livestarmobile.mobile.ui.recent

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import com.mxchip.livestarmobile.mobile.Appli
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.base.getString
import com.mxchip.livestarmobile.mobile.db.AppDatabase

import com.mxchip.livestarmobile.mobile.db.entity.CallLog
import com.mxchip.livestarmobile.mobile.db.entity.CallLogWithContact
import com.mxchip.livestarmobile.mobile.state.SystemData
import com.mxchip.livestarmobile.mobile.ui.Call
import com.mxchip.livestarmobile.mobile.ui.GroupCall
import com.mxchip.livestarmobile.mobile.ui.P2PSingleCall
import com.orhanobut.hawk.Hawk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import com.mxchip.livestarmobile.mobile.state.AppConstant
import java.util.*

class RecentViewModel(application: Application) : AndroidViewModel(application) {
    private val TAG = "RecentViewModel"
    private val userId: String = Hawk.get(AppConstant.SP_LOGIN_USER_ID)

    val callLogs: LiveData<List<CallLog>> by lazy {
        appDatabase.calllogDao().loadAllByUserIds(userId)
    }
    val callLogWithContact: LiveData<List<CallLogWithContact>> by lazy {
        appDatabase.CallLogWithContactDao().getCallLogWithContact(userId)
    }
    private val appDatabase: AppDatabase = (application as Appli).database

    fun insertCallLog(number: String) {

        val callLog = CallLog(userId = userId)

        callLog.number = number
        callLog.type = "2"
        callLog.startTime = Date()
        callLog.isCaller = true

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.calllogDao().insertAll(callLog)
        }
    }

    fun makeCall(number: String): Call {

        if (number.isNullOrBlank()) {
            throw Throwable("号码为空")
        }

        val call = when {
            number.length <= 8 -> {

                val aNumber = number.padStart(8, '0')

                if(aNumber == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(aNumber)
            }

            number.length == 11 -> {

                if(number == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(number)
            }

            number.length == 12 -> {
                if (number.contains("#")){
                    P2PSingleCall(number)
                }else{
                    GroupCall(number)
                }
            }

            number.length == 15 -> {

                if (number.contains("#")) {
                    P2PSingleCall(number)
                } else {
                    throw Throwable(getString(R.string.called_number_error))
                }
            }

            number.length == 16 -> {
                if (number.contains("#")){
                    GroupCall(number)
                }else{
                    throw Throwable(getString(R.string.called_number_error))
                }
            }
            else -> {
                throw Throwable(getString(R.string.called_number_error))
            }
        }
        return call
    }


    fun deleteCallLogById( id: Long) {
        val user = callLogs.value!!.first {
            it.uid == id
        }
        GlobalScope.launch(Dispatchers.IO) {
            appDatabase.calllogDao().delete(user)
        }

    }
}